package com.zzz.example;

import org.junit.Test;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicMarkableReference;
import java.util.concurrent.atomic.AtomicStampedReference;

public class AtomicStampReferenceDemo {

    static AtomicStampedReference<Integer> money = new AtomicStampedReference<>(19, 0);

    public static void main(String[] args) {

        for (int i = 0; i < 3; i++) {

            int stamp = money.getStamp();

            System.out.println("stamp的值是" + stamp);

            //充值线程
            new Thread(() -> {

                while (true) {

                    Integer account = money.getReference();

                    if (account < 20) {

                        if (money.compareAndSet(account, account + 20, stamp, stamp + 1)) {

                            System.out.println("余额小于20元，充值成功，目前余额：" + money.getReference() + "元");
                            break;
                        }
                    } else {

                        System.out.println("余额大于20元,无需充值");
                    }
                }
            }).start();
        }


        new Thread(() -> {    //消费线程

            for (int j = 0; j < 10; j++) {

                while (true) {

                    int timeStamp = money.getStamp();//1

                    int currentMoney = money.getReference();//39

                    if (currentMoney > 10) {
                        System.out.println("当前账户余额大于10元");
                        if (money.compareAndSet(currentMoney, currentMoney - 10, timeStamp, timeStamp + 1)) {

                            System.out.println("消费者成功消费10元，余额" + money.getReference());

                            break;
                        }
                    } else {
                        System.out.println("没有足够的金额");

                        break;
                    }
                    try {
                        Thread.sleep(1000);
                    } catch (Exception ex) {
                        ex.printStackTrace();
                        break;
                    }

                }

            }
        }).start();

    }

    @Test
    public void abaTest() throws InterruptedException {
        AtomicInteger integer = new AtomicInteger(19);

        for (int i = 0; i < 3; i++) {
            //充值线程
            new Thread(() -> {
                while (true) {
                    Integer account = integer.get();
                    if (account < 20) {
                        if (integer.compareAndSet(account, account + 20)) {
                            System.out.println("余额小于20元，充值成功，目前余额：" + integer.get() + "元");
                        }
                    }
                }
            }).start();

            new Thread(() -> {    //消费线程
                while (true) {
                    int currentMoney = integer.get();//39
                    if (currentMoney > 10) {
                        if (integer.compareAndSet(currentMoney, currentMoney - 10)) {
                            System.out.println("消费者成功消费10元，余额" + integer.get());
                        }
                    }
                }
            }).start();

            TimeUnit.SECONDS.sleep(1);
        }
    }

    @Test
    public void abaSolveTest1() throws InterruptedException {
        AtomicMarkableReference<Integer> markableReference = new AtomicMarkableReference<>(19, false);
        for (int i = 0; i < 3; i++) {
            //充值线程
            new Thread(() -> {
                while (true) {
                    Integer account = markableReference.getReference();
                    if (account < 20) {
                        if (markableReference.compareAndSet(account, account + 20, false, true)) {
                            System.out.println("余额小于20元，充值成功，目前余额：" + markableReference.getReference() + "元");
                        }
                    }
                }
            }).start();

            new Thread(() -> {    //消费线程
                while (true) {
                    int currentMoney = markableReference.getReference();//39
                    if (currentMoney > 10) {
                        if (markableReference.compareAndSet(currentMoney, currentMoney - 10, true, false)) {
                            System.out.println("消费者成功消费10元，余额" + markableReference.getReference());
                        }
                    }
                }
            }).start();

            TimeUnit.SECONDS.sleep(1);
        }
    }
}